package com.ybkj.daijia.api.member.v3;

import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.SecurityUtils;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberPaymentsRecordService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderService;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 */
@Controller("PaymentsV3Controller")
@RequestMapping(value = "member/api/rest/v3")
public class PaymentsController {

    private static final Logger logger = LoggerFactory.getLogger(PaymentsController.class);

    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberAccountService memberAccountService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private MemberPaymentsRecordService memberPaymentsRecordService;
    @Autowired
    private OrderService orderService;

    /**
     * 获取收支条目
     *
     * @param phone     用户phone
     * @param page      页数
     * @param limit     条数
     * @param appKey
     * @param token     token令牌
     * @param timestamp 时间戳
     * @return
     */
    @RequestMapping(value = "pagePaymentsByMember", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport pagePaymentsByMember(String phone, Integer page, Integer limit, String appKey,
        String token, String timestamp) {

        logger.debug("recv phone:{},page:{},limit:{},appKey:{},token:{},timestamp:{}", phone, page,
            limit, appKey, token, timestamp);

        if (null == page || null == limit || StringUtils.isBlank(phone) || SecurityUtils
            .isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

//		if(!StringUtils.isNumeric(timestamp) || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
//			return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
//		}

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(phone));
        params.add(String.valueOf(page));
        params.add(String.valueOf(limit));
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {

            Page<Map<String, Object>> records = memberPaymentsRecordService
                .pagePaymentsByMember(passenger.getId(), page, limit);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(records);

            return instance;

        } catch (Exception e) {
            logger.error("api v3 pagePaymentsByMember error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }

    /**
     * 微信公众号V5获取乘客资金流水
     *
     * @param phone   电话
     * @param page    页码
     * @param limit   条数
     * @param payType 查询流水的类型，如支付流水，返现流水（RECHARGE_TYPE_REBATE）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "pagePaymentsByMemberV5", method = RequestMethod.POST)
    public ResultTransport pagePaymentsByMemberV5(String phone, Integer page, Integer limit,
        String payType) {

        if (null == page || null == limit || StringUtils.isBlank(phone)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {
            Page<Map<String, Object>> records = memberPaymentsRecordService
                .pagePaymentsByMemberV5(passenger.getId(), page, limit, payType);
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(records);
            return instance;
        } catch (Exception e) {
            logger.error("api v3 pagePaymentsByMember error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }
    }

}
